import {
  GET_HOME_DATA,
  ADD_AIRTICLE_LIST
} from './constants.js'
import Axios from 'axios'
import {
  fromJS
} from 'immutable'
const changeHomeData = result => ({
  type: GET_HOME_DATA,
  recommendList: result.recommendList,
  articleList: result.articleList,
  topicList: result.topicList
})
export const getHomeInfo = () => {
  return dispatch => {
    Axios.get('/api/home.json').then(res => {
      if (res.data.success) {
        const result = res.data.data
        dispatch(changeHomeData(result))
      }
    })
  }
}
const addHomeList = (result, page) => ({
  type: ADD_AIRTICLE_LIST,
  list: fromJS(result),
  page
})
export const getMoreList = page => {
  return dispatch => {
    Axios.get('/api/homeList.json?page=' + (page + 1)).then(res => {
      if (res.data.success) {
        const result = res.data.data
        dispatch(addHomeList(result, page + 1))
      }
    })
  }
}